package hlhj.fhp.checkpoint.atys

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Message
import android.telephony.TelephonyManager
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpHeaders
import com.tbruyelle.rxpermissions.RxPermissions
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.iview.InitView
import hlhj.fhp.checkpoint.javaBean.MEBean
import hlhj.fhp.checkpoint.presenter.InitPresenter
import hlhj.fhp.checkpoint.utils.SPHelper
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.aty_init.*

/**
 * Created by Never Fear   on 2018\11\13 0013.
Never More....
 */
class InitAty : BaseAty<InitView,InitPresenter>(), InitView {
    private var sec=3

    private val handler= @SuppressLint("HandlerLeak")
    object :android.os.Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            countDown.text="${sec}"
            if (sec==0){
                presenter?.isLogin(this@InitAty)
            }else{
                sec--
                this.sendEmptyMessageDelayed(1,1000)
            }

        }
    }
    override fun initView() {

    }

    override fun logicStart() {
        val b = RxPermissions.getInstance(this).isGranted(Manifest.permission.READ_PHONE_STATE)
        if (b){
            handler.sendEmptyMessage(1)
        }else{
            RxPermissions.getInstance(this).request(Manifest.permission.READ_PHONE_STATE)
                    .subscribe {
                        b->
                        if (b){
                            handler.sendEmptyMessage(1)
                        }else{
                            ToastUtils.toast("请允许应用权限",this@InitAty)
                            finish()
                        }
                    }
        }
    }

    @SuppressLint("MissingSuperCall")
    override fun onDestroy() {
        super.onDestroy()
        handler.removeMessages(1)
    }
    override fun setListener() {
        btJump.setOnClickListener {
            presenter?.isLogin(this)
            handler.removeMessages(1)
        }
    }

    override fun bindView(): InitView {
        return this
    }

    override fun createPresenter(): InitPresenter {
        return InitPresenter()
    }

    override fun setMe(meBean: MEBean) {
        if (meBean.code==0){
            val intent = Intent(this, LoginAty::class.java)
            startActivity(intent)
            finish()
        }else{
            if (meBean.info.isIs_show){
                val intent = Intent(this, LoginAty::class.java)
                startActivity(intent)
                finish()
            }else{
                SPHelper.saveToken(this,meBean.info.access_token.access_token)
                val httpHeaders = HttpHeaders()
                httpHeaders.put("Authorization","Bearer ${SPHelper.getToken(this)}")
                OkGo.getInstance().addCommonHeaders(httpHeaders)

                val intent = Intent(this, HomeAty::class.java)
                startActivity(intent)
                finish()
            }
        }

    }

    @SuppressLint("MissingPermission")
    override fun judgeLogin(isLogined: Boolean) {
        if (isLogined){
            val TelephonyMgr = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            val szImei = TelephonyMgr.deviceId
            presenter?.checkImei(szImei,this)
        }else{
            val TelephonyMgr = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            val szImei = TelephonyMgr.deviceId
            presenter?.checkImei(szImei,this)
            /*val intent = Intent(this, LoginAty::class.java)
            startActivity(intent)
            finish()*/
        }
    }

    override val contentId: Int= R.layout.aty_init
}